package ta.utils;

import java.util.ArrayList;
import java.util.List;

public class TableMeta
{
    /**
     * 查询当前数据库所有表名称
     * @return list
     */
    public static List<String> getTables()
    {
    	String sql="show tables";
        List<String> list = JDBCUtils.getColunmList(sql);      
        return list;
    }

    /**
     * 根据表名返回字段属性
     * @param tableName 表名
     * @return
     */
    public static List<String[]> getColumnDatas(String tableName)
    {
        String sql = "select column_name ,data_type,column_comment from information_schema.columns where table_name = ?and table_schema=?";
       Object[] paras= {tableName,DruidConnection.getDbname()};
        List<Object[]> coldataList=JDBCUtils.getListArray(sql,paras);  
        List<String[]> list=new ArrayList<String[]>();
        for(Object[] objs:coldataList)
        {
        	String[] strs=new String[objs.length];
        	for(int i=0;i<strs.length;i++)
        	{
        		strs[0]=objs[0].toString();
        		strs[1]=mysql2Java(objs[1].toString());
        		strs[2]=objs[2].toString();        		
        	}
        	list.add(strs);
        }
        return list;
    }

    /**
     * 数据类型转化JAVA
     * @param sqlType：类型名称
     * @return
     */
    public static String mysql2Java(String sqlType) {
        if( sqlType == null || sqlType.trim().length() == 0 ) return sqlType;
        sqlType = sqlType.toLowerCase();
        switch(sqlType){
            case "nvarchar":return "String";
            case "char":return "String";
            case "varchar":return "String";
            case "text":return "String";
            case "longtext":return "String";
            case "nchar":return "String";
            case "blob":return "Byte[]";
            case "int":return "Integer";
            case "tinyint":return "Integer";
            case "smallint":return "Integer";
            case "mediumint":return "Integer";
            case "bit":return "Boolean";
            case "bigint":return "Long";
            case "float":return "Float";
            case "double":return "Double";
            case "decimal":return "java.math.BigDecimal";
            case "boolean":return "Boolean";
            case "id":return "Long";
            case "date":return "java.util.Date";
            case "datetime":return "java.util.Date";
            case "year":return "java.util.Date";
            case "time":return "java.sql.Time";
            case "timestamp":return "java.sql.Timestamp";
            case "numeric":return "java.math.BigDecimal";
            case "real":return "java.math.BigDecimal";
            case "money":return "Double";
            case "smallmoney":return "Double";
            case "image":return "Byte[]";
            case "enum":return "String";
            default:
                System.out.println("-----------------》转化失败：未发现的类型"+sqlType);
                break;
        }
        return sqlType;
    }
    
    public static void main(String[] args)
	{
    	
    	List<String[]> list=getColumnDatas("userinfo");
    	 for(String[] objs:list)
         {
         	for(String obj:objs)
         	{
         		System.out.print(obj+" ");
         	}
         	System.out.println();
         }
	}
}
